function results = mimo_precoding_analysis(varargin)
% MIMO预编码技术分析
% 输入参数:
%   'SNR_dB' - SNR范围 (dB), 默认: -5:5:30
%   'Nt', 'Nr' - 天线数量, 默认: 4x4
%   'num_realizations' - 蒙特卡洛仿真次数, 默认: 500
% 输出:
%   results - 包含预编码性能、增益等信息的结构体

% 解析输入参数
p = inputParser;
addParameter(p, 'SNR_dB', -5:5:30);
addParameter(p, 'Nt', 4);
addParameter(p, 'Nr', 4);
addParameter(p, 'num_realizations', 500);
parse(p, varargin{:});

SNR_dB = p.Results.SNR_dB;
Nt = p.Results.Nt;
Nr = p.Results.Nr;
num_realizations = p.Results.num_realizations;
SNR_linear = 10.^(SNR_dB/10);

% 添加路径
addpath('../Common');

% 获取颜色定义
colors = color_definitions();

fprintf('=== MIMO预编码技术分析 ===\n');

% 不同预编码方案比较
precoding_schemes = {'无预编码', 'MRT', 'ZF', 'MMSE'};
precoding_capacity = zeros(length(SNR_dB), length(precoding_schemes));

for snr_idx = 1:length(SNR_dB)
    snr = SNR_linear(snr_idx);
    
    for scheme_idx = 1:length(precoding_schemes)
        scheme = precoding_schemes{scheme_idx};
        
        % 蒙特卡洛仿真
        capacities = zeros(num_realizations, 1);
        
        for real = 1:num_realizations
            H_prec = sqrt(0.5) * (randn(Nr, Nt) + 1i * randn(Nr, Nt));
            
            switch scheme
                case '无预编码'
                    % 等功率分配
                    capacities(real) = sum(log2(1 + (snr/Nt) * abs(H_prec).^2));
                    
                case 'MRT'
                    % 最大比传输
                    w = H_prec' / norm(H_prec, 'fro');
                    effective_channel = H_prec * w;
                    capacities(real) = log2(1 + snr * abs(effective_channel)^2);
                    
                case 'ZF'
                    % 零强迫
                    if Nt >= Nr
                        W_zf = H_prec' * inv(H_prec * H_prec');
                        effective_channel = H_prec * W_zf;
                        capacities(real) = sum(log2(1 + snr * abs(diag(effective_channel)).^2));
                    else
                        capacities(real) = 0;
                    end
                    
                case 'MMSE'
                    % MMSE预编码
                    W_mmse = H_prec' * inv(H_prec * H_prec' + (Nt/snr) * eye(Nr));
                    effective_channel = H_prec * W_mmse;
                    capacities(real) = sum(log2(1 + snr * abs(diag(effective_channel)).^2));
            end
        end
        
        precoding_capacity(snr_idx, scheme_idx) = mean(capacities);
    end
end

% 绘制预编码性能
figure('Name', 'MIMO预编码技术', 'Position', [300, 300, 1000, 600]);

subplot(1,2,1);
for scheme_idx = 1:length(precoding_schemes)
    plot(SNR_dB, precoding_capacity(:, scheme_idx), ...
         ['-', colors(scheme_idx)], 'LineWidth', 2);
    hold on;
end
grid on;
xlabel('SNR (dB)');
ylabel('容量 (bps/Hz)');
title('不同预编码方案性能');
legend(precoding_schemes, 'Location', 'NorthWest');

% 预编码增益 (固定SNR)
snr_fixed = 15; % dB
snr_idx = find(SNR_dB == snr_fixed, 1);
subplot(1,2,2);
precoding_gains = precoding_capacity(snr_idx, :) / precoding_capacity(snr_idx, 1);
bar(precoding_gains);
grid on;
title(sprintf('预编码增益 (SNR=%d dB)', snr_fixed));
xlabel('预编码方案');
ylabel('相对增益');
set(gca, 'XTickLabel', precoding_schemes);

% 组织结果
results.SNR_dB = SNR_dB;
results.precoding_schemes = precoding_schemes;
results.precoding_capacity = precoding_capacity;
results.precoding_gains = precoding_gains;
results.snr_fixed = snr_fixed;

end